/**
 * 
 */
package hu.gbalage.owl.cms.disec.flickr;

import hu.gbalage.owl.cms.disec.flickr.auth.JDOAuthStore;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import com.aetrion.flickr.Flickr;
import com.aetrion.flickr.FlickrException;
import com.aetrion.flickr.RequestContext;
import com.aetrion.flickr.auth.Auth;
import com.aetrion.flickr.photos.Photo;
import com.aetrion.flickr.photos.PhotoList;
import com.aetrion.flickr.photos.PhotosInterface;

/**
 * @author balage
 *
 */
public class ListPhotos extends HttpServlet {

	private static final long serialVersionUID = 3973932591257518997L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		Flickr f;
		
		try {
			f = FlickrService.createFlickr();
		} catch (ParserConfigurationException e) {
			throw new ServletException(e);
		}
		
		Auth[] as = new JDOAuthStore().retrieveAll();
		
		RequestContext rc = RequestContext.getRequestContext();
		
		for(Auth a : as){
			resp.getWriter().println("Photos by "+a.getUser().getUsername());
			rc.setAuth(a);
			
			PhotosInterface pi = f.getPhotosInterface();
			PhotoList pl;
			
			try {
				pl = pi.getRecent(50, 0);
			} catch (SAXException e) {
				throw new ServletException(e);
			} catch (FlickrException e) {
				throw new ServletException(e);
			}
			
			for(Object o : pl)if (o instanceof Photo){
				Photo p = (Photo)o;
				resp.getWriter().println(p.getId()+" "+p.getLargeUrl());
			}
		}
		
	}
	
}
